package com.huarui.part.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;

import com.huarui.params.Params;
import com.huarui.part.model.MeasurementUnit;
import com.huarui.part.service.PartService;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class UtilsAction extends BaseAction {

	private static final long serialVersionUID = 2924526709280705961L;

	private MeasurementUnit unit;

	private List<MeasurementUnit> unitList;

	private PartService partService;

	private String queryParam;

	private Integer start;

	private Integer limit;

	private Map<String, Object> mesMap;

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	public MeasurementUnit getUnit() {
		return unit;
	}

	public void setUnit(MeasurementUnit unit) {
		this.unit = unit;
	}

	public String toAdd() {

		return "input";
	}

	@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "unit.name", key = "unit.name.reuqired") })
	public String doAdd() {
		unit.setName(unit.getName().trim());
		partService.createMeasurementUnit(unit);

		return SUCCESS;
	}

	public String toList() {

		unitList = partService.getAllUnits();

		return "toList";
	}

	public String toModi() {

		unit = partService.getUnitById(unit.getId());

		return "toModi";
	}

	@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "unit.name", key = "unit.name.reuqired") })
	public String doModi() {
		unit.setName(unit.getName().trim());
		partService.updateUnits(unit);
		return SUCCESS;
	}

	public String getUnits() {

		unitList = partService.getAllUnits();

		return JSON;
	}

	public String getUnitsForPage() {
		Map<String, Object> paramsMap = new HashMap<String, Object>();

		paramsMap.put("start", start == null ? 0 : start);
		paramsMap.put("pageSize", limit == null ? 10 : limit);
		mesMap = partService.getMeasurementUnitListByComplex(paramsMap);
		return SUCCESS;
	}

	public List<MeasurementUnit> getUnitList() {
		return unitList;
	}

	public void setUnitList(List<MeasurementUnit> unitList) {
		this.unitList = unitList;
	}

	public void setPartService(PartService partService) {
		this.partService = partService;
	}

	/**
	 * 后台查询
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listMeasurementUnit() {
		Map<String, Object> paramsMap = null;
		if (queryParam == null) {
			paramsMap = new HashMap<String, Object>();
		} else {
			JSONObject jo = JSONObject.fromObject(queryParam);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo,
					HashMap.class);
		}
		paramsMap.put("start", start == null ? 0 : start);
		paramsMap.put("pageSize", Params.BACKGROUND_PAGE_SIZE);
		mesMap = partService.getMeasurementUnitListByComplex(paramsMap);
		return SUCCESS;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

}
